package com.crl.module.goods.model
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	
	[Event (name="change" , type="import flash.events.Event")]
	
	
	/**
	 *  抽像分类模型;
	 * @author crl
	 * 
	 */	
	public class AbstractCategoryModel extends EventDispatcher
	{
		protected var map:Dictionary;
		public function AbstractCategoryModel()
		{
		}

		public function clear():void{
			this.map=null;
		
		}
		public function set dataProvider(value:Array):void{
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		protected function getMap():Dictionary{
			if(this.map==null){
				this.map=new Dictionary();
			}
			return this.map;
		}
		
		
		protected function add(item:*):void{
			if(item==null)return;
			var key:Object=itemKeyFunction(item);
			
			if(key==null)return;
			
			var list:Array=this.getCategory(key);
			list.push(item);
		}
		
		protected function remove(item:*):int{
			if(item==null)return -1;
			var key:Object=itemKeyFunction(item);	
			
			if(key==-1)return -1;
			
			var list:Array=this.getCategory(key);
			var index:int=list.indexOf(item);
			
			if(index==-1)return -1;
			
			list.splice(index,1);
			
			return index;
		}
		
		/**
		 *  
		 * @param item
		 * @return 
		 * 
		 */		
		protected function itemKeyFunction(item:*):Object{
			return null;
		}
		
		public function addList(items:Array):void{
			if(items==null || items.length==0)return;
			for each(var item:* in items){
				this.add(item);
			}
		}
		
		public function removes(items:Array):void{
			if(items==null || items.length==0)return;
			for each(var item:* in items){
				this.remove(item);
			}
		}
		
		public function getCategory(key:Object,isSort:Boolean=false):Array{
			var list:Array=getMap()[key] as Array;
			if(list==null){
				list=getMap()[key]=new Array();
			}
			return list;
		}
		
		public function deleteCategory(key:Object):void{
			getMap()[key]==null;
			delete getMap()[key];
		}

	}
}